/**************************************************
 * Filename: MainFrame.java
 * Version: v1.0
 * CreatedDate: 2011-11-24
 * Copyright (C) 2011 By cafebabe.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * If you would like to negotiate alternate licensing terms, you may do
 * so by contacting the author: talentyao@foxmail.com
 ***************************************************/
 
package com.train.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.http.client.HttpClient;

import com.train.TrainClient;
import com.train.TrainQueryInfo;

/**
 * 功能描述
 * @author cafebabe
 * @since 2011-11-24 
 * @version 1.0
 */
public class MainFrame {

	private JFrame frame;
	private TableDataAdapter dataModel;
	private HttpClient httpClient;
	private JTextField txtFrom;
	private JTextField txtTo;
	private JTextField txtStartDate;
	private String timeSlotStr;
	private TrainClient client;


	/**
	 * Create the application.
	 */
	public MainFrame(HttpClient httpClient) {
		this.httpClient = httpClient;
		client = new TrainClient(httpClient);
		dataModel = new TableDataAdapter();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {		
		frame = new JFrame();
		frame.setBounds(100, 100, 911, 591);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("出发地：");
		label.setBounds(10, 52, 48, 15);
		frame.getContentPane().add(label);
		
		txtFrom = new JTextField();
		txtFrom.setBounds(57, 49, 66, 21);
		frame.getContentPane().add(txtFrom);
		txtFrom.setColumns(10);
		
		JLabel label_1 = new JLabel("目的地：");
		label_1.setBounds(144, 52, 48, 15);
		frame.getContentPane().add(label_1);
		
		txtTo = new JTextField();
		txtTo.setBounds(191, 49, 66, 21);
		frame.getContentPane().add(txtTo);
		txtTo.setColumns(10);
		
		JLabel label_2 = new JLabel("出发日期：");
		label_2.setBounds(278, 52, 66, 15);
		frame.getContentPane().add(label_2);
		
		txtStartDate = new JTextField();
		txtStartDate.setBounds(339, 49, 66, 21);
		frame.getContentPane().add(txtStartDate);
		txtStartDate.setColumns(10);
		
		JLabel label_3 = new JLabel("出发时间：");
		label_3.setBounds(415, 52, 66, 15);
		frame.getContentPane().add(label_3);
		
		Object[] timeSlot = {"00:00--24:00","00:00--06:00","06:00--12:00","12:00--18:00","18:00--24:00"};
		JComboBox comboBox = new JComboBox(timeSlot);
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				System.out.println(e.getItem());
				timeSlotStr = (String) e.getItem();
				action(2);
			}
		});
		comboBox.setBounds(476, 49, 106, 21);
		frame.getContentPane().add(comboBox);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				action(1);
			}
		});
		button.setBounds(592, 48, 93, 23);
		frame.getContentPane().add(button);		
		
		JTable table = new JTable();
		table.setModel(dataModel);		
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane scrollPane = new JScrollPane(table);
		table.setPreferredScrollableViewportSize(new Dimension(550, 200));
		
		scrollPane.setBounds(10, 107, 883, 440);
		frame.getContentPane().add(scrollPane);
		frame.setVisible(true);
	
	}

	/**
	 * 功能描述
	 * @param i 
	 */
	protected void action(int evenKey) {
		switch (evenKey) {
			case 2:
				
				break;
			case 1:
				String from = txtFrom.getText();
//				String fromCode = com.train.Util.getCityCode(from.trim());
				String to = txtTo.getText();
//				String toCode = com.train.Util.getCityCode(to.trim());
				String date = txtStartDate.getText().trim();
				List<TrainQueryInfo> tableData = client.queryTrain(from, to, date, timeSlotStr);
				ArrayList<ArrayList<Object>> rows = new ArrayList<ArrayList<Object>>();
				for (TrainQueryInfo tqInfo : tableData) {
					ArrayList<Object> row = new ArrayList<Object>();
					row.add(tqInfo.getTrainNo());
					row.add(tqInfo.getFromStation());
					row.add(tqInfo.getToStation());
					row.add(tqInfo.getTakeTime());
					row.add(tqInfo.getBuss_seat());
					row.add(tqInfo.getBest_seat());
					row.add(tqInfo.getOne_seat());
					row.add(tqInfo.getTwo_seat());
					row.add(tqInfo.getVag_sleeper());
					row.add(tqInfo.getSoft_sleeper());
					row.add(tqInfo.getHard_sleeper());
					row.add(tqInfo.getSoft_seat());
					row.add(tqInfo.getHard_seat());
					row.add(tqInfo.getNone_seat());
					row.add(tqInfo.getOther_seat());
					rows.add(row);
				}
				String[] columnNames = new String[] {
						"车次", "发站", "到站", "历时", "商务座", "特等座", "一等座", 
						"二等座", "高级软卧", "软卧", "硬卧", "软座", "硬座",
						"无座", "其他"
					};
				dataModel.refurbish(rows, columnNames, null);
				break;

			default:
				break;
		}
		
	}
}
